var mapHistory = [];
var CityTarget;

$(function () {
    var h=document.body.clientHeight;
    // console.log(0.7*h+'px')
    $(".changeTab").css('height',0.5*h+'px');
    $("#myMap1").css('height',0.6*h+'px')
    var city = $('#citySelect').val();
    initStatics("",-1,1);
    initCloum('',-1,1);
    initmap1(1);


//鼠标事
    $("#loansBalance,#loansAmount,#loansEvaluatePrice,#buildArea,#estateCount,#dynamicMortgageRate").click(function(){
        var zoneNumber= $("#zoneNumber").val();
        window.location.href="/mortgage/searchMortgage?city="+zoneNumber;
    });
    $('#monthResult').click(function () {
        var zoneNumber= $("#zoneNumber").val();
        var province = $("#province").val();
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth()+1;
        var firstDate = year+"年"+month+"月"+"01日";
        var lastDay = getLastDay(year,month);
        var lastDate = year+"年"+month+"月"+lastDay+"日";
//获取一个月的最后一天
        function getLastDay(year,month){
            var new_year = year;        //取当前的年份
            var new_month = month++;    //取下一个月的第一天，方便计算（最后一天不固定）
            if(month>12){                //如果当前大于12月，则年份转到下一年
                new_month -=12;            //月份减
                new_year++;                //年份增
            }
            var new_date = new Date(new_year,new_month,1);   // 取当年当月的 下个 月中的第一天
// 获取当月最后一天日期
            return (new Date(new_date.getTime()-1000*60*60*24)).getDate();
        }

        $(this).attr("href","/mortgage/searchMortgage?city="+zoneNumber+"&province="+province+"&createdDateStart="+firstDate+"&createdDateEnd="+lastDate)
    })

})
//  5个
var initStatics=function (city,zoneNumber,level) {
// 从后台取数据
    $.ajax({
        type: "POST",
        url: "/main/statistics",
        data: {
            city: city,
            zoneNumber:zoneNumber,
            level:level
        },
        success: function (msg) {
            var staticsData=msg.dataResult[0];
            var staticsLtv= (staticsData['loansBalance']/staticsData['loansEvaluatePrice']*100).toFixed(2);
            var creditBalance = (staticsData['loansEvaluatePrice']-staticsData['loansBalance']);
            if(msg.dataResult==null){
                return
            }
            $("#loansAmount").text(transition(staticsData['loansAmount'] || 0)+unit(staticsData['loansAmount'])+'元')
            $("#loansBalance").text(transition(staticsData['loansBalance'] || 0)+unit(staticsData['loansBalance'])+'元')
            $("#loansEvaluatePrice").text(transition(staticsData['loansEvaluatePrice'] || 0)+unit(staticsData['loansEvaluatePrice'])+'元')
            $("#buildArea").text(transition(staticsData['buildArea'] || 0)+unit(staticsData['buildArea'])+'平方米')
            $("#estateCount").text(transition(staticsData['estateCount'])+unit(staticsData['estateCount'])+"笔")
            $("#monthResult").html("本月新增"+transition(staticsData['monthResult'])+unit(staticsData['monthResult'])+"笔")
            if(isNaN(staticsLtv)){
                staticsLtv = 0
            }
            // $("#dynamicMortgageRate").text(staticsLtv+'%')
            $("#dynamicMortgageRate").text(transition(creditBalance || 0)+unit(creditBalance)+'元')
        }
    })

}
function initData(data, key) {
    if(data['estateCount']==[]||data['loansBalance']==[]||data['dynamicMortgageRate']==[] || data['antiFraud']==[]){
        return
    }
    var xData = [];
    var series1 = [];
    var series2 = [];
    var series3 = [];
    if(key==1){

        for (var i = 0; i < data['estateCount'].length; i++) {
            var obj={}
            var obj1={}
            obj.value=data['estateCount'][i]['allCount']
            obj1.value=data['estateCount'][i]['estateCount']
            obj.zoneNumber=data['estateCount'][i]['zoneNumber']
            obj1.zoneNumber=data['estateCount'][i]['zoneNumber']
            xData.push(data['estateCount'][i]['cityName'])
            series1.push(obj)
            series2.push(obj1)
        }
    }
    else if (key == 2) {
        for (var i = 0; i < data['loansBalance'].length; i++) {
            var obj={}
            var obj1={}
            obj.value=transitiony(data['loansBalance'][i]['loansAmountTotal'])
            obj1.value=transitiony(data['loansBalance'][i]['loansBalanceTotal'])
            obj.zoneNumber=data['loansBalance'][i]['zoneNumber']
            obj1.zoneNumber=data['loansBalance'][i]['zoneNumber']
            xData.push(data['loansBalance'][i]['cityName'])
            series1.push(obj)
            series2.push(obj1)
        }
    }
    else if (key == 3) {
        for (var i = 0; i < data['dynamicMortgageRate'].length; i++) {
            var obj={}
            var obj1={}
            var obj2={}
            obj.value=transitiony(data['dynamicMortgageRate'][i]['loansEvaluatePrice'])
            obj1.value=transitiony(data['dynamicMortgageRate'][i]['loansBalance'])
            obj2.value=data['dynamicMortgageRate'][i]['dynamicMortgageRate']
            obj.zoneNumber=data['dynamicMortgageRate'][i]['zoneNumber']
            obj1.zoneNumber=data['dynamicMortgageRate'][i]['zoneNumber']
            obj2.zoneNumber=data['dynamicMortgageRate'][i]['zoneNumber']
            xData.push(data['dynamicMortgageRate'][i]['cityName'])
            series1.push(obj)
            series2.push(obj1)
            series3.push(obj2)
        }
    }
    else if (key == 4) {
        for (var i = 0; i < data['antiFraud'].length; i++) {
            var obj={}
            var obj1={}
            obj.value=data['antiFraud'][i]['estateCount']
            obj1.value=data['antiFraud'][i]['antiFraudCount']
            obj.zoneNumber=data['antiFraud'][i]['zoneNumber']
            obj1.zoneNumber=data['antiFraud'][i]['zoneNumber']
            xData.push(data['antiFraud'][i]['cityName'])
            series1.push(obj)
            series2.push(obj1)
        }
    }
    var initDa ={}
    initDa[0]=xData
    initDa[1]=series1
    initDa[2]=series2
    initDa[3]=series3
    return initDa;
}
var mapMax1,mapMax2,mapMax3,mapMax4,mapMin2,mapMin3,mapMin4,dataResult;
var initCloum=function (city,zoneNumber,level) {
// 从后台取出数据
    var cloumnarData=''
    $.ajax({
        type: "POST",
        url: "/main/columnarData1",
        async:false,
        data: {
            city: city,
            zoneNumber:zoneNumber,
            level:level
        },
        success: function (data) {
            cloumnarData=data,
                {"dataResult":{"loansBalance":[],"dynamicMortgageRate":[]}}
            dataResult=cloumnarData['dataResult']
            var sdata1=initData(dataResult,1)
            var sdata2=initData(dataResult,2)
            var sdata3=initData(dataResult,3)
            var sdata4=initData(dataResult,4)
            function getOption(legend,xdata,series1,series2,key,flag,threeColor) {

                var option1=
                    {
                        backgroundColor: '#fff',
                        legend: {
                            top: 20,
                            textStyle:{
                                color:'#AFA79F',
                            },
                            selectedMode:false,
                            data: legend
                        },
                        title: {
                            subtext: '单位：笔',
                            left: '5%',
                            top: '2%',
                            textStyle: {
                                color: '#333'
                            }
                        },
                        grid: {
                            left: '3%',
                            right: '4%',
                            bottom: '2%',
                            containLabel: true
                        },
                        tooltip: {
                            show:"true",
                            trigger: 'axis',
                            axisPointer: { // 坐标轴指示器，坐标轴触发有效
                                type: 'shadow' // 默认为直线，可选为：'line' | 'shadow'
                            },
                            formatter: function(params){
                                var ltv='';
                                if(key==2){
                                    ltv='占比率'
                                }else if(key==3){
                                    ltv='动态抵押率'
                                }
                                var relVal = params[0].name;
                                var all = "";
                                var value = 0;
                                if(key ==4){
                                    all+= relVal+"<br/>"+params[0].seriesName +": "+params[0].value+"<br/>"+params[1].seriesName +": "+params[1].value
                                }else{
                                    if(!params[1]){
                                        return all+=  relVal+"<br/>"+params[0].seriesName +": "+params[0].value;
                                    }
                                    if(params[0].value && params[1].value) {
                                        all+= relVal+"<br/>"+params[0].seriesName +": "+params[0].value+"<br/>"+params[1].seriesName +": "+params[1].value
                                            +'</br>'+ltv+': '+(100*parseFloat(params[0].value/parseFloat(params[1].value))).toFixed(2) + "%";

                                    }else if(params[0].value==0 || params[1].value==0){
                                        all+= relVal+"<br/>"+params[0].seriesName +": "+'-'+"<br/>"+params[1].seriesName +": "+'-'
                                            +'</br>'+ltv+': '+'-' + "%";
                                    }else if(!params[1].value) {
                                        all+=  relVal+"<br/>"+params[0].seriesName +": "+params[0].value;
                                    }else if(!params[0].value){
                                        all+=  relVal+"<br/>"+params[1].seriesName +": "+params[1].value;
                                    }
                                }
                                return all;
// all = relVal+"<br/>"+params[0].seriesName +": "+params[0].value+"<br/>"+params[1].seriesName +": "+params[1].value;
                            }
                        },
                        xAxis:  {
                            type: 'value',
                            axisTick : {show: false},
                            axisLine: {
                                show: true,
                                lineStyle:{
                                    color:'#AFA79F',
                                    type:'dotted'
                                }
                            },
                            splitLine: {
                                show:true,
                                lineStyle:{type:'dotted'}
                            }
                        },
                        yAxis: [
                            {
                                type: 'category',
                                axisTick : {show: false},
                                axisLine: {
                                    show: true,
                                    lineStyle:{
                                        color:'#AFA79F',
                                        type:'dotted'
                                    }
                                },
                                splitLine: {
                                    show:true,
                                    lineStyle:{type:'dotted'}
                                },
                                data: xdata
                            },
                            {
                                type: 'category',
                                axisLine: {show:false},
                                axisTick: {show:false},
                                axisLabel: {show:false},
                                splitArea: {show:false},
                                splitLine: {show:false},
                                data: xdata
                            },

                        ],
                        series: [
                            {
                                name: legend[0],
                                type: 'bar',
                                yAxisIndex:1,

                                itemStyle:{
                                    normal: {
                                        show: true,
                                        color: '#006EE5',
// barBorderRadius:50,
                                        borderWidth:0,
// borderColor:'#333',
                                    }
                                },
                                barGap:'0%',
                                barCategoryGap:'50%',
                                label: {
                                    normal: {
                                        position: 'right',
                                        show: true
                                    }
                                },
                                data: series1
                            },

                            {
                                name: legend[1],
                                type: 'bar',
                                itemStyle:{
                                    normal: {
                                        show: true,
                                        color: '#B0D4F2',
// barBorderRadius:50,
                                        borderWidth:0,
// borderColor:'#333',
                                    }
                                },
                                barGap:'0%',
                                barCategoryGap:'50%',
                                data: series2
                            }

                        ]
                    };
                if(flag){
                    option1.series[0].itemStyle.normal.color = '#FD6818'
                    option1.series[1].itemStyle.normal.color = '#FBB590'
                    option1.title.subtext = '单位：亿元';
                }
                if(threeColor){
                    option1.series[0].itemStyle.normal.color = '#FFB400'
                    option1.series[1].itemStyle.normal.color = '#FBD77F'
                    option1.title.subtext = '单位：亿元';
                }
                if(flag&&threeColor){
                    option1.series[0].itemStyle.normal.color = '#FFB400'
                    option1.series[1].itemStyle.normal.color = '#FBD77F'
                    option1.title.subtext = '单位：笔';
                }
                return option1
            }
//渲染三个柱状图
//押品数量柱状图
            var myChart1 = echarts.init($("#tab-1").get(0));
            var myChart2 = echarts.init($("#tab-2").get(0));
            var myChart3 = echarts.init($("#tab-3").get(0));
            var myChart4 = echarts.init($("#tab-4").get(0));
            for(var i=0,data0=[];i<sdata1[2].length;i++){
                data0.push(sdata1[2][i].value);
            }
            for(var i=0,data1=[];i<sdata2[2].length;i++){
                data1.push(sdata2[2][i].value);
                // console.log(data1)
            }
            for(var i=0,data2=[];i<sdata3[3].length;i++){
                data2.push(sdata3[3][i].value);
                // console.log(data2)
            }
            for(var i=0,data3=[];i<sdata4[2].length;i++){
                data3.push(sdata4[2][i].value);
                // console.log(data2)
            }
            mapMax1 = Math.ceil(Math.max.apply(0,data0));
            mapMax2 = Math.ceil(Math.max.apply(0,data1));
            mapMax3 = Math.ceil(Math.max.apply(0,data2)*100);
            mapMax4 = Math.ceil(Math.max.apply(0,data3));
            myChart1.setOption(getOption(["贷款笔数"],sdata1[0],sdata1[2]));

            window.onresize = function () {
                myChart1.resize();
            };
            myChart2.setOption(getOption(["贷款总额","贷款余额"],sdata2[0],sdata2[1],sdata2[2],2,true));
            window.onresize = function () {
                myChart2.resize();
            };
            var zoneNumber= $("#zoneNumber").val()
            if(level==2||level==1){
                myChart1.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber2;
                });
                myChart2.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber2;
                });
                myChart3.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber2;
                });
                myChart4.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber2;
                });
            }else if(level==3){
                myChart1.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber+"&district="+zoneNumber2;
                });
                myChart2.on('click',function(params){
                    var zoneNumber2 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber+"&district="+zoneNumber2;
                });
                myChart3.on('click',function(params){
                    var zoneNumber3 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber+"&district="+zoneNumber3;
                });
                myChart4.on('click',function(params){
                    var zoneNumber3 = params.data.zoneNumber;
                    window.location.href = "/mortgage/searchMortgage?city="+zoneNumber+"&district="+zoneNumber3;
                });
            }

            myChart3.setOption(getOption(["估值价格","贷款余额"],sdata3[0],sdata3[1],sdata3[2],3,"",true));
            window.onresize = function () {
                myChart3.resize();
            };
            myChart4.setOption(getOption(["总数","反欺诈数"],sdata4[0],sdata4[1],sdata4[2],4,true,true));
            window.onresize = function () {
                myChart4.resize();
            };
        }

    })
}
var initmap1 = function (type,city) {
    require.config({
        paths: {
            echarts: 'https://echartsjs.com/build/dist'
        }
    });
// 使用
    require(
        [
            'echarts',
            'echarts/chart/map', // 使用柱状图就加载bar模块，按需加载
        ],
        function (ec) {
// 基于准备好的dom，初始化echarts图表
            var myMap1 = ec.init(document.getElementById('myMap1'));
            var ecConfig = require('echarts/config');
            var zrEvent = require('zrender/tool/event');
            var curIndx = 0;
            //全国省份
            var mapType = [
                'china',
// 23个省
                '广东', '青海', '四川', '海南', '陕西',
                '甘肃', '云南', '湖南', '湖北', '黑龙江',
                '贵州', '山东', '江西', '河南', '河北',
                '山西', '安徽', '福建', '浙江', '江苏',
                '吉林', '辽宁',
// 5个自治区
                '新疆', '广西', '宁夏', '内蒙古', '西藏',
// 4个直辖市
                '北京', '天津', '上海', '重庆',
// 2个特别行政区

            ];
            //全国城市
            var cityMap = {
                "北京市": "110100",
                "天津市": "120100",
                "上海市": "310100",
                "重庆市": "500100",

                "崇明县": "310200", //
                "湖北省直辖县市": "429000", //
                "铜仁市": "522200", //
                "毕节市": "522400", //

                "石家庄市": "130100",
                "唐山市": "130200",
                "秦皇岛市": "130300",
                "邯郸市": "130400",
                "邢台市": "130500",
                "保定市": "130600",
                "张家口市": "130700",
                "承德市": "130800",
                "沧州市": "130900",
                "廊坊市": "131000",
                "衡水市": "131100",
                "太原市": "140100",
                "大同市": "140200",
                "阳泉市": "140300",
                "长治市": "140400",
                "晋城市": "140500",
                "朔州市": "140600",
                "晋中市": "140700",
                "运城市": "140800",
                "忻州市": "140900",
                "临汾市": "14100",
                "吕梁市": "141100",
                "呼和浩特市": "150100",
                "包头市": "150200",
                "乌海市": "150300",
                "赤峰市": "150400",
                "通辽市": "150500",
                "鄂尔多斯市": "150600",
                "呼伦贝尔市": "150700",
                "巴彦淖尔市": "150800",
                "乌兰察布市": "150900",
                "兴安盟": "152200",
                "锡林郭勒盟": "152500",
                "阿拉善盟": "152900",
                "沈阳市": "210100",
                "大连市": "210200",
                "鞍山市": "210300",
                "抚顺市": "210400",
                "本溪市": "210500",
                "丹东市": "210600",
                "锦州市": "210700",
                "营口市": "210800",
                "阜新市": "210900",
                "辽阳市": "211000",
                "盘锦市": "211100",
                "铁岭市": "211200",
                "朝阳市": "211300",
                "葫芦岛市": "211400",
                "长春市": "220100",
                "吉林市": "220200",
                "四平市": "220300",
                "辽源市": "220400",
                "通化市": "220500",
                "白山市": "220600",
                "松原市": "220700",
                "白城市": "220800",
                "延边朝鲜族自治州": "222400",
                "哈尔滨市": "230100",
                "齐齐哈尔市": "230200",
                "鸡西市": "230300",
                "鹤岗市": "230400",
                "双鸭山市": "230500",
                "大庆市": "230600",
                "伊春市": "230700",
                "佳木斯市": "230800",
                "七台河市": "230900",
                "牡丹江市": "23100",
                "黑河市": "231100",
                "绥化市": "231200",
                "大兴安岭地区": "232700",
                "南京市": "320100",
                "无锡市": "320200",
                "徐州市": "320300",
                "常州市": "320400",
                "苏州市": "320500",
                "南通市": "320600",
                "连云港市": "320700",
                "淮安市": "320800",
                "盐城市": "320900",
                "扬州市": "321000",
                "镇江市": "321100",
                "泰州市": "321200",
                "宿迁市": "321300",
                "杭州市": "330100",
                "宁波市": "330200",
                "温州市": "330300",
                "嘉兴市": "330400",
                "湖州市": "330500",
                "绍兴市": "330600",
                "金华市": "330700",
                "衢州市": "330800",
                "舟山市": "330900",
                "台州市": "331000",
                "丽水市": "331100",
                "合肥市": "340100",
                "芜湖市": "340200",
                "蚌埠市": "340300",
                "淮南市": "340400",
                "马鞍山市": "340500",
                "淮北市": "340600",
                "铜陵市": "340700",
                "安庆市": "340800",
                "黄山市": "341000",
                "滁州市": "341100",
                "阜阳市": "341200",
                "宿州市": "341300",
                "六安市": "341500",
                "亳州市": "341600",
                "池州市": "341700",
                "宣城市": "341800",
                "福州市": "350100",
                "厦门市": "350200",
                "莆田市": "350300",
                "三明市": "350400",
                "泉州市": "350500",
                "漳州市": "350600",
                "南平市": "350700",
                "龙岩市": "350800",
                "宁德市": "350900",
                "南昌市": "360100",
                "景德镇市": "360200",
                "萍乡市": "360300",
                "九江市": "360400",
                "新余市": "360500",
                "鹰潭市": "360600",
                "赣州市": "360700",
                "吉安市": "360800",
                "宜春市": "360900",
                "抚州市": "361000",
                "上饶市": "361100",
                "济南市": "370100",
                "青岛市": "370200",
                "淄博市": "370300",
                "枣庄市": "370400",
                "东营市": "370500",
                "烟台市": "370600",
                "潍坊市": "370700",
                "济宁市": "370800",
                "泰安市": "370900",
                "威海市": "37100",
                "日照市": "371100",
                "莱芜市": "371200",
                "临沂市": "371300",
                "德州市": "371400",
                "聊城市": "371500",
                "滨州市": "371600",
                "菏泽市": "371700",
                "郑州市": "410100",
                "开封市": "410200",
                "洛阳市": "410300",
                "平顶山市": "410400",
                "安阳市": "410500",
                "鹤壁市": "410600",
                "新乡市": "410700",
                "焦作市": "410800",
                "濮阳市": "410900",
                "许昌市": "411000",
                "漯河市": "411100",
                "三门峡市": "411200",
                "南阳市": "411300",
                "商丘市": "411400",
                "信阳市": "411500",
                "周口市": "411600",
                "驻马店市": "411700",
                "省直辖县级行政区划": "469000",
                "武汉市": "420100",
                "黄石市": "420200",
                "十堰市": "420300",
                "宜昌市": "420500",
                "襄阳市": "420600",
                "鄂州市": "420700",
                "荆门市": "420800",
                "孝感市": "420900",
                "荆州市": "421000",
                "黄冈市": "421100",
                "咸宁市": "421200",
                "随州市": "421300",
                "恩施土家族苗族自治州": "422800",
                "长沙市": "430100",
                "株洲市": "430200",
                "湘潭市": "430300",
                "衡阳市": "430400",
                "邵阳市": "430500",
                "岳阳市": "430600",
                "常德市": "430700",
                "张家界市": "430800",
                "益阳市": "430900",
                "郴州市": "431000",
                "永州市": "431100",
                "怀化市": "431200",
                "娄底市": "431300",
                "湘西土家族苗族自治州": "433100",
                "广州市": "440100",
                "韶关市": "440200",
                "深圳市": "440300",
                "珠海市": "440400",
                "汕头市": "440500",
                "佛山市": "440600",
                "江门市": "440700",
                "湛江市": "440800",
                "茂名市": "440900",
                "肇庆市": "441200",
                "惠州市": "441300",
                "梅州市": "441400",
                "汕尾市": "441500",
                "河源市": "441600",
                "阳江市": "441700",
                "清远市": "441800",
                "东莞市": "441900",
                "中山市": "442000",
                "潮州市": "445100",
                "揭阳市": "445200",
                "云浮市": "445300",
                "南宁市": "450100",
                "柳州市": "450200",
                "桂林市": "450300",
                "梧州市": "450400",
                "北海市": "450500",
                "防城港市": "450600",
                "钦州市": "450700",
                "贵港市": "450800",
                "玉林市": "450900",
                "百色市": "451000",
                "贺州市": "451100",
                "河池市": "451200",
                "来宾市": "451300",
                "崇左市": "451400",
                "海口市": "460100",
                "三亚市": "460200",
                "三沙市": "460300",
                "成都市": "510100",
                "自贡市": "510300",
                "攀枝花市": "510400",
                "泸州市": "510500",
                "德阳市": "510600",
                "绵阳市": "510700",
                "广元市": "510800",
                "遂宁市": "510900",
                "内江市": "511000",
                "乐山市": "511100",
                "南充市": "511300",
                "眉山市": "511400",
                "宜宾市": "511500",
                "广安市": "511600",
                "达州市": "511700",
                "雅安市": "511800",
                "巴中市": "511900",
                "资阳市": "512000",
                "阿坝藏族羌族自治州": "513200",
                "甘孜藏族自治州": "513300",
                "凉山彝族自治州": "513400",
                "贵阳市": "520100",
                "六盘水市": "520200",
                "遵义市": "520300",
                "安顺市": "520400",
                "黔西南布依族苗族自治州": "522300",
                "黔东南苗族侗族自治州": "522600",
                "黔南布依族苗族自治州": "522700",
                "昆明市": "530100",
                "曲靖市": "530300",
                "玉溪市": "530400",
                "保山市": "530500",
                "昭通市": "530600",
                "丽江市": "530700",
                "普洱市": "530800",
                "临沧市": "530900",
                "楚雄彝族自治州": "532300",
                "红河哈尼族彝族自治州": "532500",
                "文山壮族苗族自治州": "532600",
                "西双版纳傣族自治州": "532800",
                "大理白族自治州": "532900",
                "德宏傣族景颇族自治州": "533100",
                "怒江傈僳族自治州": "533300",
                "迪庆藏族自治州": "533400",
                "拉萨市": "540100",
                "昌都地区": "542100",
                "山南地区": "542200",
                "日喀则地区": "542300",
                "那曲地区": "542400",
                "阿里地区": "542500",
                "林芝地区": "542600",
                "西安市": "610100",
                "铜川市": "610200",
                "宝鸡市": "610300",
                "咸阳市": "610400",
                "渭南市": "610500",
                "延安市": "610600",
                "汉中市": "610700",
                "榆林市": "610800",
                "安康市": "610900",
                "商洛市": "611000",
                "兰州市": "620100",
                "嘉峪关市": "620200",
                "金昌市": "620300",
                "白银市": "620400",
                "天水市": "620500",
                "武威市": "620600",
                "张掖市": "620700",
                "平凉市": "620800",
                "酒泉市": "620900",
                "庆阳市": "62100",
                "定西市": "621100",
                "陇南市": "621200",
                "临夏回族自治州": "622900",
                "甘南藏族自治州": "623000",
                "西宁市": "630100",
                "海东地区": "632100",
                "海北藏族自治州": "632200",
                "黄南藏族自治州": "632300",
                "海南藏族自治州": "632500",
                "果洛藏族自治州": "632600",
                "玉树藏族自治州": "632700",
                "海西蒙古族藏族自治州": "632800",
                "银川市": "640100",
                "石嘴山市": "640200",
                "吴忠市": "640300",
                "固原市": "640400",
                "中卫市": "640500",
                "乌鲁木齐市": "650100",
                "克拉玛依市": "650200",
                "吐鲁番地区": "652100",
                "哈密地区": "652200",
                "昌吉回族自治州": "652300",
                "博尔塔拉蒙古自治州": "652700",
                "巴音郭楞蒙古自治州": "652800",
                "阿克苏地区": "652900",
                "克孜勒苏柯尔克孜自治州": "653000",
                "喀什地区": "653100",
                "和田地区": "653200",
                "伊犁哈萨克自治州": "654000",
                "塔城地区": "654200",
                "阿勒泰地区": "654300",
                "自治区直辖县级行政区划": "659000",
                "台湾省": "71000",
                "香港特别行政区": "810100",
                "澳门特别行政区": "820000"
            };
            var mapGeoData = require('echarts/util/mapData/params');
            for (var city in cityMap) {
                mapType.push(city);
// 自定义扩展图表类型
                mapGeoData.params[city] = {
                    getGeoJson: (function (c) {
                        var geoJsonName = cityMap[c];
                        return function (callback) {
                            $.getJSON('/assets/main/geoJson/china-main-city/' + geoJsonName + '.json', callback);
                        }
                    })(city)
                }
            }
            var text = '';
            if (type == 1) {
                text = "贷款笔数"
            }else if (type == 2) {
                text = "贷款余额"
            }
            else if (type == 3) {
                text = "余额价值比"
            }else if(type==4){
                text = "命中数量"
            }
            var mapChangeHandler = function (param) {
                var len = mapType.length;
                var mt = param.target;
                console.log(mt)
                var f = false;
                var mt1
                for (var i = 0; i < len; i++) {
                    if (mt == mapType[i]) {
                        f = true;
                        mt = mapType[i];
                    }
                }

                var provinceName=['广东', '青海', '四川', '海南', '陕西',
                    '甘肃', '云南', '湖南', '湖北', '黑龙江',
                    '贵州', '山东', '江西', '河南', '河北',
                    '山西', '安徽', '福建', '浙江', '江苏',
                    '吉林', '辽宁']
                for (var i = 0; i < provinceName.length; i++) {
                    if (mt == provinceName[i]) {
                        mt1 = mt+'省';
                    }
                }
                if(mt=="新疆"){
                    mt1="新疆维吾尔自治区"
                }else if(mt=="广西"){
                    mt1="广西壮族自治区"
                }else if (mt=="宁夏"){
                    mt1="宁夏回族自治区"
                }else if (mt=="西藏"){
                    mt1="西藏自治区"
                }else if (mt=="内蒙古"){
                    mt1="内蒙古自治区"
                }
                var cityZone;
                var s= false;
                var m= $("#tab1").hasClass('active');
                var zhixia=['北京','上海','重庆','天津']

                for (var i = 0; i < zhixia.length; i++) {
                    if (mt == zhixia[i]) {
                        mt = mt+'市';
                        s=true
                    }
                }
                for ( var key in cityMap) {
//通过遍历对象属性的方法，遍历键值对，获得key，然后通过 对象[key]获得对应的值
                    name = key;
                    if(mt==name){
                        s=true;
                        value = cityMap[key];
                        cityZone=value
                    }
                }
                var funcF=function(){
                    if (!f) {//返回全国地图
                        // $('#zoneNumber').val('-1')
                        // initCloum("",-1,1)
                        // initStatics("",-1,1)
                        // option.title.text = "全国地图";
                        // option.title.subtext = "";
                        //跳转聚合图
                        window.location.href="/map/cluster1?currentMap=clusterMap&flag=true&city="+CityTarget+ "&district=" + mt;
                        // mt = 'china';
                    } else if(f&&!s){//点击的是省
// 柱状图的联动
                        $("#province").val(mt1);
                        initCloum(mt1,'',2)
                        initStatics(mt1,'',2)
                        option.title.text = mt + "地图";
                    }else if(f&&s){//点击的是市
                        $('#zoneNumber').val(cityZone)
                        CityTarget = zoneNumber.value;
                        initCloum('',cityZone,3)
                        initStatics('',cityZone,3)
                        option.title.text = mt + "地图";
                    }
                    if(param.target != mapHistory[mapHistory.length - 1].target){
                        mapHistory.push(param);
                    }
                    if(mapHistory.length > 1){
                        $('#myMap1-backbutton').text('返回' + mapHistory[mapHistory.length - 2].target + '地图');
                    }else{
                        $('#myMap1-backbutton').text('全国地图');
                    }

                }()

                if(m){
                    // mapMax = Math.max.apply(null,initData(dataResult,2)[2]);
                    option.dataRange.max = mapMax1;
                }else if($("#tab2").hasClass('active')){
                    option.dataRange.max = mapMax2;
                }else if($("#tab3").hasClass('active')){
                    option.dataRange.max = mapMax3;
                }else if($("#tab4").hasClass('active')){
                    option.dataRange.max = mapMax4;
                }
                option.tooltip.trigger = 'item';
                option.series[0].mapType = mt;
                myMap1.setOption(option, true);
                // console.log(params.name)
            }

            //点击地图切换
            myMap1.on(ecConfig.EVENT.MAP_SELECTED, mapChangeHandler);
            //地图回退处理函数
            $('#myMap1-backbutton').on('click', function () {
                if(mapHistory.length < 2)return;
                //删除当前记录
                mapHistory.splice(mapHistory.length - 1, 1);
                mapChangeHandler(mapHistory[mapHistory.length - 1]);
            });
            option = {
                title: {
                    text: '全国地图',
                    x: 'center',
                },
                tooltip: {
                    trigger: 'item',
                    formatter:'{b0}</br> '+text+':  {c0}笔<br />',
                },

                //颜色渐变
                dataRange: {
                    min: 0,
                    max: mapMax1,
                    x: 'left',
                    y: 'bottom',
                    text:['高','低'],           // 文本，默认为数值文本
                    calculable : true,
                    // color:['#00b6dd','#1ABCDF','#33C2E0','#4DC8E2','#67CEE3','#80D3E5','#9AD9E6','#B3DFE8','#cde5e9']
                    // color:['#006edd','#cde5e9']
                    color:['#006EE5','#B0D4F2']
                },
                toolbox: {
                    show: true,
                    orient: 'vertical',
                    x: 'right',
                    y: 'center',
                },
                series: [{
                    name: text,
                    type: 'map',
                    mapType: 'china',
                    selectedMode: 'single',
                    itemStyle: {
                        normal: {
                            color:'#dee8ee',//地图背景色
                            borderColor: '#fff',//边框颜色
                            borderWidth:'1',
                            label: {
                                show: true,
                                textStyle: {
                                    color: "#fff"//当前字体
                                }
                            },
                        },
                        emphasis: {
                            color: '#ddd000',//划过北京颜色
                            label: {
                                show: true,
                                textStyle: {
                                    color: "#fff",//高亮字体
                                }
                            },
                        },
                    },
                    data: []
                },],
                textStyle:{
                    color: 'red',
                }
            };

            $('#tab02').click(function () {
                var city = $('#citySelect').val();
                $.ajax({
                    type: "POST",
                    url: "/main/mapData1",
                    data: {
                        type: 2
                    },
                    success: function (msg) {
                        option.tooltip.formatter='{b0}</br> 贷款余额:  {c0}亿元<br />'
                        option.series[0].data = convertData(msg.mapData);
                        // option.dataRange.color = ["#00b6dd","#cbf1f9"];
                        option.dataRange.color = ["#FD6818","#FBB590"];
                        option.dataRange.max = mapMax2;
// 为echarts对象加载数据
                        myMap1.setOption(option);
                    }
                })
            })
            $('#tab01').click(function () {
                var city = $('#citySelect').val();
                $.ajax({
                    type: "POST",
                    url: "/main/mapData1",
                    data: {
                        type: 1
                    },
                    success: function (msg) {

                        option.tooltip.formatter='{b0}</br> 押品数量:  {c0}笔<br />'
                        option.series[0].data = convertData3(msg.mapData);
                        option.dataRange.max = mapMax1;
                        // 为echarts对象加载数据
                        myMap1.setOption(option);
                    }
                })
            })
            $('#tab03').click(function () {
                $.ajax({
                    type: "POST",
                    url: "/main/mapData1",
                    data: {
                        type: 3
                    },
                    success: function (msg) {
                        var convertData3 = function (data) {
                            var res = [];
                            var req = []
                            for (var i = 0; i < data.length; i++) {
                                var name = ''
                                if(data[i].cityName=="北京市"||data[i].cityName=="重庆市"||
                                    data[i].cityName=="上海市"||data[i].cityName=="天津市"||data[i].cityName.indexOf('省') >= 0){
                                    name = data[i].cityName.substr(0, data[i].cityName.length - 1)
                                }
                                else {
                                    name = data[i].cityName
                                }
                                req.push(data[i].value)
                                res.push({
                                    name: name,
                                    value: data[i].value
                                });
                            }
                            option.dataRange.color = ["#FFB400","#FBD77F"];
                            // console.log(option.dataRange)
                            option.dataRange.max = mapMax3;

                            return res;
                        };
                        option.tooltip.formatter='{b0}</br> 动态抵押率:  {c0}%<br />'
                        option.series[0].data = convertData3(msg.mapData);
// 为echarts对象加载数据
                        myMap1.setOption(option);
                    }
                })
            })
            $('#tab04').click(function () {
                var city = $('#citySelect').val();
                $.ajax({
                    type: "POST",
                    url: "/main/mapData1",
                    data: {
                        type: 4
                    },
                    success: function (msg) {
                        option.tooltip.formatter='{b0}</br> 反欺诈数:  {c0}笔<br />'
                        option.series[0].data = convertData0(msg.mapData);
                        // option.dataRange.color = ["#00b6dd","#cbf1f9"];
                        option.dataRange.color = ["#FFB400","#FBD77F"];
                        option.dataRange.max = mapMax4;
// 为echarts对象加载数据
                        myMap1.setOption(option);
                    }
                })
            })
            $.ajax({
                type: "POST",
                url: "/main/mapData1",
                data: {
                    type: type
                },
                success: function (msg) {

                    option.series[0].data = convertData0(msg.mapData);
                    mapHistory.push({
                        target:'全国'
                        ,selected: convertData0(msg.mapData)
                    })
                    // 为echarts对象加载数据
                    myMap1.setOption(option);
                    window.onresize = function () {
                        myMap1.resize();
                    };
                }
            })
            var convertData0 = function (data) {
                var res = [];
                var req = [];
                for (var i = 0; i < data.length; i++) {
                    var name = ''
                    if(data[i].cityName=="北京市"||data[i].cityName=="重庆市"||
                        data[i].cityName=="上海市"||data[i].cityName=="天津市"||data[i].cityName.indexOf('省') >= 0){
                        name = data[i].cityName.substr(0, data[i].cityName.length - 1)
                    }
                    else {
                        name = data[i].cityName
                    }
                    req.push(data[i].value);
                    res.push({
                        name: name,
                        value: data[i].value,
                    });
                }
                return res;
            };
            var convertData = function (data) {
                var res = [];
                var req = [];
                for (var i = 0; i < data.length; i++) {
                    var name = '';
                    if(data[i].cityName=="北京市"||data[i].cityName=="重庆市"||
                        data[i].cityName=="上海市"||data[i].cityName=="天津市"||data[i].cityName.indexOf('省') >= 0){
                        name = data[i].cityName.substr(0, data[i].cityName.length - 1);
                    }
                    else {
                        name = data[i].cityName
                    }
                    req.push(transitiony(data[i].value));
                    res.push({
                        name: name,
                        value: transitiony(data[i].value)
                    });
                }
                return res;
            };
            var convertData3 = function (data) {
                var res = [];
                var req = []
                for (var i = 0; i < data.length; i++) {
                    var name = ''
                    if(data[i].cityName=="北京市"||data[i].cityName=="重庆市"||
                        data[i].cityName=="上海市"||data[i].cityName=="天津市"||data[i].cityName.indexOf('省') >= 0){
                        name = data[i].cityName.substr(0, data[i].cityName.length - 1)
                    }
                    else {
                        name = data[i].cityName
                    }
                    req.push(data[i].value)
                    res.push({
                        name: name,
                        value: data[i].value
                    });
                }
                option.dataRange.color = ['#006EE5','#B0D4F2'];
                return res;
            };
        }
    );
// window.onresize = function () {
//     myMap1.resize();
// };

}
